import { getSalary, saveSalary, updateSalary } from '../../../api/salary';

Page({
  data: {
    id: 0,
    name: '',
    date: '2024-07', // 默认日期
    projects: [],

    workingTime: 0,
    workingSalary: 0,
    overTime: 0,
    overSalary: 0,

    kilometers: 0,
    monovalent: 0,
    travelExpenses: 0,
    advance: 0,
    total: 0,
    showEditModal: false,
    dayCosts:0,
    overtimeCosts:0
  },

  onLoad: function(options) {
    const { name, id } = options;
    if (id && name) {
      const parsedId = parseInt(id, 10);
      if (!isNaN(parsedId)) {
        const date = new Date().toISOString().slice(0, 7); 
        this.setData({
          name: name,
          id: parsedId,
          date: date
        });
        this.fetchSalaryData(parsedId, date);
      } else {
        console.error('Invalid ID format');
      }
    } else {
      console.error('ID or name is missing in options');
    }
  },

  bindDateChange: function(e) {
    const date = e.detail.value;
    this.setData({
      date: date
    });
    this.fetchSalaryData(this.data.id, date);
  },

  fetchSalaryData: function(id, date) {
    getSalary(id, date).then(response => {
      console.log(response.data);
      if (response.code === 1 && response.data) {
        const salaryData = response.data;
        this.setData({
          workingTime: salaryData.workingTime || 0,
          workingSalary: salaryData.workingSalary || 0,
          overTime: salaryData.overTime || 0,
          overSalary: salaryData.overSalary || 0,
          kilometers: salaryData.kilometers || 0,
          monovalent: salaryData.monovalent || 0,
          travelExpenses: salaryData.travelExpenses || 0,
          advance: salaryData.advance || 0,
          total: salaryData.total || 0
        });
      } else {
        console.error('No salary data found');
      }
    }).catch(error => {
      console.error('Error fetching salary data:', error);
    });
  },

  showEditModal: function() {
    this.setData({
      showEditModal: true
    });
  },

  cancelEditModal: function() {
    this.setData({
      showEditModal: false
    });
  },

  confirmEdit: function() {
    // 重新计算总工资   
    const total = (this.data.workingTime * this.data.dayCosts) + 
                  (this.data.overTime * this.data.overtimeCosts) +
                  (this.data.kilometers * this.data.monovalent) +
                  this.data.travelExpenses - this.data.advance;

    const salaryDto = {
      employeeId: this.data.id,
      time: this.data.date,
      workingTime: this.data.workingTime,
      overTime: this.data.overTime,
      dayCosts: this.data.dayCosts,
      overtimeCosts: this.data.overtimeCosts,
      advance: this.data.advance,
      kilometers: this.data.kilometers,
      monovalent: this.data.monovalent,
      travelExpenses: this.data.travelExpenses,
      total: total
    };

    // 调用后端接口更新数据
    updateSalary(salaryDto).then(res => {
      if (res.code === 1) {
        console.log(res.data);
        this.setData({
          total: total,
          showEditModal: false
        });
        this.fetchSalaryData(this.data.id, this.data.date); // 重新获取数据并渲染
      } else {
        console.error('Failed to update salary data');
      }
    }).catch(err => {
      console.error('Error updating salary data:', err);
    });
  },

  inputWorkingSalary: function(e) {
    this.setData({
      dayCosts: parseFloat(e.detail.value) || 0
    });
  },

  inputMonovalent: function(e) {
    this.setData({
      monovalent: parseFloat(e.detail.value) || 0
    });
  },

  inputOverSalary: function(e) {
    this.setData({
      overtimeCosts: parseFloat(e.detail.value) || 0
    });
  },

  inputTravelExpenses: function(e) {
    this.setData({
      travelExpenses: parseFloat(e.detail.value) || 0
    });
  },

  inputAdvance: function(e) {
    this.setData({
      advance: parseFloat(e.detail.value) || 0
    });
  },

  calculate: function() {
    const total = (this.data.workingTime * this.data.dayCosts) + 
                  (this.data.overTime * this.data.overtimeCosts) +
                  (this.data.kilometers * this.data.monovalent) +
                  this.data.travelExpenses - this.data.advance;
    // const workingSalary = this.data.workingTime * this.data.workingSalary
    // const overSalary = this.data.overTime * this.data.overSalary
    this.setData({
      total: total,
      // workingSalary: workingSalary,
      // overSalary : overSalary
    });

    const salaryDto = {
      employeeId: this.data.id,
      time: this.data.date,
      workingTime: this.data.workingTime,
      overTime: this.data.overTime,
      dayCosts: this.data.dayCosts,
      overtimeCosts: this.data.overtimeCosts,
      advance: this.data.advance,
      kilometers: this.data.kilometers,
      monovalent: this.data.monovalent,
      travelExpenses: this.data.travelExpenses,
      total: this.data.total
    };

    saveSalary(salaryDto).then(res => {
      if (res.data.code === 1 && res.data.data) {
        this.setData({
          total: res.data.data.total // 更新总计数据
        });
      } else {
        console.error('Failed to calculate salary');
      }
    }).catch(err => {
      console.error('Error calculating salary:', err);
    });
  }
});
